package control;
import model.*;
import db.*;

import java.text.SimpleDateFormat;
import java.util.*;

/*This is the Case controller.
 * Its purpose is to give instructions towards manipulating the data concerning cases, including adding, updating, closing
 * or simply returning a case for inspection. 
 */

public class CaseCtrl 
{
	// Private fields.
	
	private DBCase dbCase = new DBCase();
	private DBDebtor dbDebtor = new DBDebtor();
	private DBClient dbClient = new DBClient();
	private DBVehicle dbVehicle = new DBVehicle();
	private DBCaseXEmployee dbCaseEmp = new DBCaseXEmployee();
	
	public CaseCtrl()
	{
		
	}
	
	//This method returns all the cases in the database in an ArrayList of type Case.
	
	public ArrayList<Case> listAllCases()
	{
		return dbCase.getAllCases(false);
	}
	
	//This method returns one case, searching for it by using the ID.
	
	public Case getCaseById(String id)
	{
		return dbCase.getCaseById(id,true);
	}
	
	//This method searches for an active case by looking for the assigned vehicle's license plate.
	
	public Case getCaseByLicensePlate(String licensePlate)
	{
		return dbCase.getCaseByLicensePlate(licensePlate, true);
	}
	
	//Creating a case can be done by calling this specific method. The parameters are for filling the fields of the Case object.
	
	public void createCase(String clientCvr, String debtorCpr, String licensePlate, String information, String debt, String contract) throws Exception
	{
		String timeStamp = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
		Case toInsert = new Case();
		toInsert.setClient(dbClient.getClientByCvr(clientCvr, false));
		toInsert.setDebtor(dbDebtor.getDebtorCpr(debtorCpr, false));
		toInsert.setVehicle(dbVehicle.getVehicleByLicensePlate(licensePlate, true));
		toInsert.setDebt(debt);
		toInsert.setContract(contract);
		toInsert.setProcessed("Opened");
		toInsert.setInformation(information);
		toInsert.setStartDate(timeStamp);
		dbCase.insertCase(toInsert);
	}
	
	//Updating a case is made possible by using this method. As with create, the parameters are the fields of the Case.
	
	public int updateCase(String id, String extensionDate, String information, String historyArea, String endDate, boolean closed, String debt)
	{
		Case toUpdate = dbCase.getCaseById(id, false);
		toUpdate.setExtensionDate(extensionDate);
		toUpdate.setInformation(information);
		toUpdate.setHistoryArea(historyArea);
		toUpdate.setDebt(debt);
		if(closed)
		{
			toUpdate.setEndDate(endDate);
			toUpdate.setProcessed("Closed");
		}
		return dbCase.updateCase(toUpdate);
	}
	
	//This method marks a case as "closed". This means that the case has either been resolved or cannot be completed.
	
	public int closeCase(String id)
	{
		Case toClose = dbCase.getCaseById(id,true);
		toClose.setProcessed("0");
		return dbCase.updateCase(toClose);
	}
	
	public boolean deleteCase(String _id)
	{
		return dbCase.deleteCase(_id);
	}
	
	public void deleteCaseAssociations(String _caseId)
	{
		dbCaseEmp.deleteCaseAssociations(_caseId);
	}
}
